/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Entity.DauSach;
import Util.DatabaseHandle;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Dark_Knight
 */
public class DauSachImpl implements DauSachDAO {

    public boolean add(DauSach data) {
        String sql = "{call sp_DAUSACH_add(?,?,?,?,?)}";
        DatabaseHandle db = new DatabaseHandle(sql, true);
        boolean flag;
        try {
            db.setParameter(1, data.getMaDauSach());
            db.setParameter(2, data.getMaTacGia());
            db.setParameter(3, data.getMaNXB());
            db.setParameter(4, data.getMaTheLoai());
            db.setParameter(5, data.getTenSach());
            flag = db.executeStorePro();
            db.closed();            
        } catch (SQLException ex) {
            Logger.getLogger(DauSachImpl.class.getName()).log(Level.SEVERE, null, ex);
            db.closed();
            return false;
        }                
        return flag;
    }

    public boolean delete(int MaDauSach) {
        String sql = "{call sp_DAUSACH_delete(?)}";
        DatabaseHandle db = new DatabaseHandle(sql, true);
        boolean flag;
        try {
            db.setParameter(1, MaDauSach);
            flag = db.executeStorePro();
            db.closed();            
        } catch (SQLException ex) {
            Logger.getLogger(DauSachImpl.class.getName()).log(Level.SEVERE, null, ex);
            db.closed();
            return false;
        }
        return flag;
    }

    public boolean edit(DauSach data) {
        String sql = "{call sp_DAUSACH_edit(?,?,?,?,?)}";
        DatabaseHandle db = new DatabaseHandle(sql, true);
        boolean flag;
        try {
            db.setParameter(1, data.getMaDauSach());
            db.setParameter(2, data.getMaTacGia());
            db.setParameter(3, data.getMaNXB());
            db.setParameter(4, data.getMaTheLoai());
            db.setParameter(5, data.getTenSach());
            flag = db.executeStorePro();
            db.closed();
        } catch (SQLException ex) {
            Logger.getLogger(DauSachImpl.class.getName()).log(Level.SEVERE, null, ex);
            db.closed();
            return false;
        }  
        return flag;
    }

    public ResultSet getAll() {
        String sql = "{call sp_DAUSACH_getAll()}";
        DatabaseHandle db = new DatabaseHandle(sql, false);
        return db.excuteQuery();
    }        
}
